<template>
  <van-button
    v-if="isFollowed"
    class="follow-btn"
    type="default"
    round
    size="small"
    @click="doFollow"
    :loading="loading"
    >已关注</van-button
  >
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    @click="doFollow"
    :loading="loading"
    >关注</van-button
  >
</template>

<script>
import { addFollow, delFollow } from '@/api/user.js';

export default {
  name: 'FollowUser',
  // v-model 数据和事件
  model: {
    prop: 'isFollowed', // 默认值：'value'
    event: 'change', // 默认事件：'input'
  },
  props: {
    // 关注状态
    isFollowed: {
      required: true,
      type: Boolean,
    },
    // 作者id
    authorId: {
      required: true,
      type: [String, Number],
    },
  },
  data() {
    return {
      loading: false,
    };
  },
  methods: {
    // 执行 关注/取消关注 操作
    async doFollow() {
      // 显示加载中
      this.loading = true;
      try {
        if (this.isFollowed) {
          await delFollow(this.authorId);
        } else {
          await addFollow(this.authorId);
        }
        this.$toast.success('操作成功: )');
        this.$emit('change', !this.isFollowed);
      } catch (err) {
        console.log('catch代码块：------------');
        console.dir(err);
        this.$toast.fail('操作失败: (');
      } finally {
        this.loading = false;
      }
    },
  },
  computed: {},
  watch: {},
  components: {},
};
</script>

<style lang="less" scoped>
</style>
